<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>promise和ajax</title>
</head>
<body>
    
    <script>


        // promise 和 ajax
        function promiseAjax ({url='',type='get',dataType='json'}) {
            return new Promise((resolve,reject)=>{
                let xhr = new XMLHttpRequest();
                xhr.open(type,url,true);
                xhr.responseType = dataType;
                xhr.onload = function () { // readyState = 4 , status = 200
                    resolve(response);
                }

                xhr.onerror = function (err) {  
                    reject(err);
                }

                xhr.send();
            });
        }

        promiseAjax({}).then((data)=>{
            console.log(data);
        },(err)=>{
            console.log(error);
        });

        
        
        /*
        
            $.ajax{
                url: '',
                type: 'get',
                dataType: 'json',
                success: 
            }
        
        */
    </script>
</body>
</html>